package xx.iverson.school_manage.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xx.iverson.school_manage.entity.OperationRecord;
import xx.iverson.school_manage.log.Loggable;
import xx.iverson.school_manage.mapper.OperationRecordMapper;
import xx.iverson.school_manage.mapper.PointsMapper;
import xx.iverson.school_manage.service.OperationRecordService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author wei77
 */
@Service
public class OperationRecordServiceImpl implements OperationRecordService {

    @Resource
    private OperationRecordMapper operationRecordMapper;

    @Resource
    private PointsMapper pointsMapper;

    @Transactional
    @Override
    @Loggable(value = "增加积分操作记录")
    public boolean addIncreaseOperationRecord(OperationRecord operationRecord) {
        boolean operationRecordSuccess = operationRecordMapper.insertIncreaseOperationRecord(operationRecord);
        boolean pointsSuccess = pointsMapper.insertPoints(operationRecord.getUserId(), operationRecord.getOperatorPoints());
        return operationRecordSuccess && pointsSuccess;
    }

    @Transactional
    @Override
    @Loggable(value = "减少积分操作记录")
    public boolean addDecreaseOperationRecord(OperationRecord operationRecord) {
        boolean operationRecordSuccess = operationRecordMapper.insertDecreaseOperationRecord(operationRecord);
        boolean pointsSuccess = pointsMapper.decreasePoints(operationRecord.getUserId(), operationRecord.getOperatorPoints());
        return operationRecordSuccess && pointsSuccess;
    }

    @Transactional
    @Override
    @Loggable(value = "批量增加积分操作记录")
    public boolean batchAddIncreaseOperationRecord(List<OperationRecord> operationRecords) {
        int totalOperations = operationRecords.size();
        int successfulOperations = 0;

        for (OperationRecord record : operationRecords) {
            if (operationRecordMapper.insertIncreaseOperationRecord(record)) {
                if (pointsMapper.insertPoints(record.getUserId(), record.getOperatorPoints())) {
                    successfulOperations++;
                }
            }
        }

        return successfulOperations == totalOperations;
    }

    @Transactional
    @Override
    @Loggable(value = "批量减少积分操作记录")
    public boolean batchAddDecreaseOperationRecord(List<OperationRecord> operationRecords) {
        int totalOperations = operationRecords.size();
        int successfulOperations = 0;

        for (OperationRecord record : operationRecords) {
            if (operationRecordMapper.insertDecreaseOperationRecord(record)) {
                if (pointsMapper.decreasePoints(record.getUserId(), record.getOperatorPoints())) {
                    successfulOperations++;
                }
            }
        }

        return successfulOperations == totalOperations;
    }

    @Override
    @Loggable(value = "获取操作记录")
    public List<OperationRecord> getOperationRecords(Integer userId) {
        return operationRecordMapper.selectOperationRecord(userId);
    }

    @Override
    @Loggable(value = "根据月份获取操作记录")
    public List<OperationRecord> findOperationRecordsByMonth(int year, int month,int userId) {
        return operationRecordMapper.findOperationRecordsByMonth(year, month,userId);
    }

    @Override
    @Loggable(value = "根据月份导出操作记录")
    public List<OperationRecord> exportOperationRecordsByMonth(int start, int end, int userId) {
        return operationRecordMapper.exportOperationRecordsByMonth(start, end,userId);
    }

    @Override
    @Loggable(value = "根据月份导出多个用户的操作记录")
    public HashMap<String, List<OperationRecord>> exportMultipleOperationRecordsByMonth(int startDate, int endDate, List<Integer> userIds) {
        // 查询所有用户在指定时间范围内的操作记录
        List<OperationRecord> allRecords = operationRecordMapper.findRecordsByMonth(startDate, endDate);

        // 分组操作记录，按用户ID和realName组合成的键分组
        HashMap<String, List<OperationRecord>> recordsByUser = new HashMap<>();

        allRecords.stream()
                .filter(record -> userIds.contains(record.getUserId()))
                .forEach(record -> {
                    String key = record.getRealName() + "—" + record.getUserId();
                    recordsByUser.computeIfAbsent(key, k -> new ArrayList<>()).add(record);
                });

        // 返回按用户ID分组的操作记录列表
        return recordsByUser;
    }
}